package com.topdt.jbpm.web.struts.action;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.thx.common.struts2.CurdAction;
import com.topdt.jbpm.identity.User;
import com.topdt.jbpm.model.Work;
import com.topdt.jbpm.service.WorkService;
import com.topdt.jbpm.web.SessionUserUtil;

@Namespace(value = "/workflow")
@Action(value = "haveTodo", results = { @Result(name = "list", location = "/workflow/haveTodo/list.jsp"),
		@Result(name = "list_oneApp", location = "/workflow/haveTodo/list_oneApp.jsp"),
		@Result(name = "list_lending", location = "/workflow/haveTodo/list_lending.jsp") }, interceptorRefs = {
		@InterceptorRef("topDtStack"),
		@InterceptorRef(value = "pageInterceptor", params = { "includeMethods", "doListByAppId" }),
		@InterceptorRef(value = "pageInterceptor", params = { "includeMethods", "haveTodoRunWork" }) })
@Controller
@Scope("prototype")
public class HaveTodoAction extends CurdAction<Work> {

	private static final long serialVersionUID = 1L;
	private String appId;// fengjinshou 20151221
	private String officeDocTypeAppIds;// fengjinshou 20151223 公文类型id
	private String startorTitle;// fengjinshou 20151230 列表上显示的发起人表头
	private String appName;// fengjinshou 20151230 应用名
	private String state;

	@Autowired
	private WorkService workService;

	@Override
	public void doList() {
		logger.debug("<进入>:doList方法");
		if (StringUtils.isNotBlank(entity.getWorkTitle()) && entity.getWorkTitle().equals("请输入标题名")) {
			entity.setWorkTitle(null);
		}
		User user = SessionUserUtil.getCurrentUser();
		if (StringUtils.isBlank(officeDocTypeAppIds)) {
			list = workService.getWorkPage(user.getId(), entity.getWorkTitle(), entity.getWorkType());
		} else {// fengjinshou 20151223 根据应用范围查找
			list = workService.getWorkPage(user.getId(), entity.getWorkTitle(), entity.getWorkType(), officeDocTypeAppIds);
		}
	}

	/**
	 * 根据appId搜索
	 *
	 * @author 冯晋首
	 * @date 2015年12月21日 下午8:06:56
	 */
	public String doListByAppId() {
		logger.debug("<进入>:doListByAppId方法    通过应用id查找已办工作");
		if (appId != null && !appId.equals("") && !appId.equals("null")) {
			if (StringUtils.isNotBlank(entity.getWorkTitle()) && entity.getWorkTitle().equals("请输入标题名")) {
				entity.setWorkTitle(null);
			}
			User user = SessionUserUtil.getCurrentUser();
			// fengjinshou 20151221 按应用id查找已办工作
			list = workService.getWorkPage(user.getId(), entity.getWorkTitle(), entity.getWorkType(), appId);
		}
		if (StringUtils.isNotBlank(appName) && appName.indexOf("借阅") != -1) {
			return "list_lending";
		}
		return "list_oneApp";
	}

	/**
	 * 查询正在办理的工作或已结束工作
	 *
	 * @author 刘杰
	 * @date 2016年1月8日 上午10:59:28
	 * @return
	 */
	public String haveTodoRunWork() {
		logger.debug("<进入>:haveTodoRunWork方法    查询正在办理的工作或已结束工作");
		if (StringUtils.isNotBlank(entity.getWorkTitle()) && entity.getWorkTitle().equals("请输入标题名")) {
			entity.setWorkTitle(null);
		}
		User user = SessionUserUtil.getCurrentUser();
		if (StringUtils.isBlank(officeDocTypeAppIds)) {
			if (StringUtils.isNotBlank(appId)) {
				list = workService.getHaveTodoRunWorkPage(user.getId(), entity.getWorkTitle(), entity.getWorkType(), appId, state);
				if (StringUtils.isNotBlank(appName) && appName.indexOf("借阅") != -1) {
					return "list_lending";
				} else {
					return "list_oneApp";
				}
			} else {
				list = workService.getHaveTodoRunWorkPage(user.getId(), entity.getWorkTitle(), entity.getWorkType(), state);
			}

		} else {
			list = workService.getHaveTodoRunWorkPage(user.getId(), entity.getWorkTitle(), entity.getWorkType(), officeDocTypeAppIds, state);
		}
		return "list";
	}

	public String getAppId() {
		return appId;
	}

	public void setAppId(String appId) {
		this.appId = appId;
	}

	public String getOfficeDocTypeAppIds() {
		return officeDocTypeAppIds;
	}

	public void setOfficeDocTypeAppIds(String officeDocTypeAppIds) {
		this.officeDocTypeAppIds = officeDocTypeAppIds;
	}

	public String getStartorTitle() {
		return startorTitle;
	}

	public void setStartorTitle(String startorTitle) {
		this.startorTitle = startorTitle;
	}

	public String getAppName() {
		return appName;
	}

	public void setAppName(String appName) {
		this.appName = appName;
	}

	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}

}
